﻿/*************************************************************************/
/** Copyright.                                                          **/
/** FileName: MeasureA.c                                                **/
/** Author: kongls                                                      **/
/** Version: 1.0.0.0                                                    **/
/** Description: measure A for leq withou headfile                      **/
/*************************************************************************/
//=======================================================================//
//= Include files.                                                      =//
//=======================================================================//
#include "DemoProc.h"
#include "HMI_Engine.h"
#include "SGUI_Basic.h"

extern char time_str[64];
extern LEQ_DATA_EVENT_DATA  LeqData;
extern char battery_str[3];

//=======================================================================//
//= Macro definition.                                                   =//
//=======================================================================//

//=======================================================================//
//= Static function declaration.                                        =//
//=======================================================================//
static HMI_ENGINE_RESULT    HMI_MeasureA_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_MeasureA_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_MeasureA_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT    HMI_MeasureA_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_ACTION       s_stMeasureAPaintActions =  {   NULL,
                                                        HMI_MeasureA_Prepare,
                                                        HMI_MeasureA_RefreshScreen,
                                                        HMI_MeasureA_ProcessEvent,
                                                        HMI_MeasureA_PostProcess
                                                    };
//=======================================================================//
//= Global variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_OBJECT       g_stHMIMeasureAPaint =      {   HMI_SCREEN_ID_MeasureA_PAINT,
                                                        &s_stMeasureAPaintActions
                                                    };

//=======================================================================//
//= Function define.                                                    =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_MeasureA_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    SGUI_Basic_ResetMask(pstDeviceIF);
    if(NULL != pstDeviceIF->fnClear)
    {
        printf("fnclear\n");
        pstDeviceIF->fnClear();
    }
    else
    {
        printf("DrawRectangle1\n");
        SGUI_Basic_DrawRectangle1(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
    }
    SGUI_Basic_ResetMask(pstDeviceIF);
    HMI_MeasureA_RefreshScreen(pstDeviceIF, pstParameters);

    printf("HMI_MeasureA_Prepare\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_MeasureA_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    printf("2 HMI_MeasureA_RefreshScreen\n");
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_INT                iPaintBkgIdx;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
   
    // Paint background.
    // for(iPaintBkgIdx=0; iPaintBkgIdx<pstDeviceIF->stSize.iHeight; iPaintBkgIdx+=5)
    // {
    //     SGUI_Basic_DrawHorizontalLine(pstDeviceIF, 0, pstDeviceIF->stSize.iWidth-1, iPaintBkgIdx, SGUI_COLOR_FRGCLR);
    // }
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_MeasureA_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }

    //printf("3 HMI_MeasureA_ProcessEvent id=%d\n", *piActionID);
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    HMI_ENGINE_RESULT           eProcessResult;
    SGUI_INT                    iProcessAction;
    KEY_PRESS_EVENT*            pstKeyEvent;
    // LEQ_EVENT*            pstKeyEvent;
    char cLeq[8];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    eProcessResult =            HMI_RET_NORMAL;
    iProcessAction =            HMI_DEMO_PROC_NO_ACT;
    pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent; // (KEY_PRESS_EVENT*)pstEvent;
  
    if(EVENT_ID_KEY_PRESS == pstEvent->iID)
    {
        printf("%s:%d EVENT_ID_KEY_PRESS keyvalue=%d\n", __FILE__, __LINE__, pstKeyEvent->Data.uiKeyValue);

        switch(pstKeyEvent->Data.uiKeyValue)
        {
            case KEY_VALUE_HOME:
            {
                // Event data is invalid.
                eProcessResult = HMI_RET_INVALID_DATA;
                break;
            }
            case KEY_VALUE_MENU:
            {
                // go to main menu
                printf("menu\n");
                iProcessAction = HMI_DEMO_PROC_CANCEL;
                break;
            }
            case KEY_VALUE_PAUSE:
            {
                // hold / unhold
                LeqData.uHoldStatus = !LeqData.uHoldStatus;
                printf("hold/unhold\n");
                break;
            }
            case KEY_VALUE_ENTER:
            {
                // FS
                LeqData.uTimeWeightType = !LeqData.uTimeWeightType;
                printf("Fast/Slow\n");
                break;
            }
            // case KEY_VALUE_LEFT:
            // {
            //     printf("to menu screen\n");
            //     // do nothing
            //     //HMI_GoToScreen(HMI_SCREEN_ID_MeasureB_PAINT, NULL);
            //     break;
            // }
            case KEY_VALUE_RIGHT:
            case KEY_VALUE_LEFT:
            {
                printf("to second screen\n");
                // go to first screen
                HMI_GoToScreen(HMI_SCREEN_ID_MeasureB_PAINT, NULL);
                //iProcessAction = HMI_DEMO_PROC_CANCEL;
                break;
            }
            case KEY_VALUE_UP:
            {
                printf("up acz=%d\n",LeqData.uWeightType);
                if(++LeqData.uWeightType > 2) 
                    LeqData.uWeightType = 0;
                break;
            }
            case KEY_VALUE_DOWN:
            {
                printf("down acz=%d\n", LeqData.uWeightType);
                if(--LeqData.uWeightType < 0)
                    LeqData.uWeightType = 2;
                break;
            }
            default:
            {
                break;
            }
        }
    }

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 1, 1, 120, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, battery_str, &SGUI_DEFAULT_FONT_16, 91, 1, 127, SGUI_DRAW_NORMAL);

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "SPL", &SGUI_DEFAULT_FONT_16, 1,  30, 30, SGUI_DRAW_NORMAL);
    // display in backline
    //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);

    if(0 == LeqData.uTimeWeightType)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Slow", &SGUI_DEFAULT_FONT_16, 80, 30, 127, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Fast", &SGUI_DEFAULT_FONT_16, 80, 30, 127, SGUI_DRAW_NORMAL);

    // current Hold status
    if(0 == LeqData.uHoldStatus)  // unhold
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, " ", &SGUI_DEFAULT_FONT_16, 1, 135, 40, SGUI_DRAW_NORMAL);
    }
    else  // hold
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Hold", &SGUI_DEFAULT_FONT_16, 1, 135, 40, SGUI_DRAW_NORMAL);
    }

    // leq 
    switch (LeqData.uWeightType)
    {
    case 0:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqA / 10);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);
        break;
    case 1:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqC / 10);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "C",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);
        break;
    case 2:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqZ / 10);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Z",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);
        break;
    default:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqA / 10);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);
        break;
    }

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq, &SGUI_DEFAULT_FONT_48, 2, 60, 127, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "dB", &SGUI_DEFAULT_FONT_16, 100, 108, 127, SGUI_DRAW_NORMAL);

    // over/low
    if(LeqData.uLeqA > 900)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "over", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else if(LeqData.uLeqA < 400)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "low", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    if(NULL != piActionID)
    {
        *piActionID = iProcessAction;
    }
    return eProcessResult;
}

HMI_ENGINE_RESULT HMI_MeasureA_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
    //printf("4 HMI_MeasureA_PostProcess time=%s\n\n", time_str);
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 110, 91, 120, SGUI_DRAW_NORMAL);
    //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "93", &SGUI_DEFAULT_FONT_16, 91, 1, 127, SGUI_DRAW_NORMAL);

    //printf(" basic  post process  id=%d\n", iActionID);
    if(HMI_PROCESS_SUCCESSFUL(eProcResult))
    {
        if(HMI_DEMO_PROC_CANCEL == iActionID)
        {
            HMI_GoBack(NULL);
        }
    }

    return HMI_RET_NORMAL;
}



